package com.huilan.hgt.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.huilan.hgt.util.Constants;
import com.huilan.hgt.util.DateUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;


/**
 * 自动填充数据库公共字段
 *
 * @author ghy
 * @date 2018-07-11 下午5:08
 **/
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        Object originalObject = metaObject.getOriginalObject();
        Object creater = getFieldValByName("creater", metaObject);
        Object createTime = getFieldValByName("createTime", metaObject);
        if (creater == null) {
            Integer userId = Constants.getCurrentUser().getUserId();
            setFieldValByName("creater", userId.toString(), metaObject);
        }
        if (createTime == null) {
            setFieldValByName("createTime", DateUtil.getNowDate(), metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Object originalObject = metaObject.getOriginalObject();

//        Object updater = getFieldValByName("updater", metaObject);
//        Object updateTime = getFieldValByName("updateTime", metaObject);
//        if (updater == null) {
//            setFieldValByName("updater", 11, metaObject);
//        }
//        if (updateTime == null) {
//            setFieldValByName("updateTime", new Date(), metaObject);
//        }
    }
}
